/*
 * Copyright (C) 2015 The Sven Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.core.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Tan on 14-3-20.
 * email hellotanm@gmail.com
 */
@SuppressWarnings("unused")
public class DateUtil {

    public static final String[] pattens = new String[]{
        "yyyy-MM-dd HH:mm:ss.SSS",
        "yyyy-MM-dd HH:mm:ss",
        "yyyy-MM-dd HH:mm",
        "yyyy-MM-dd"
    };
    
    public static Date parseDate(String json, DateFormat df)  {
        try {
            if (json == null || json.trim().length() == 0) {
                return null;
            }
            
            return df.parse(json);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static Date parseDate(String json) throws ParseException {
        for (String s : pattens) {
            DateFormat df = new SimpleDateFormat(s);
            Date date = parseDate(json, df);
            if (date != null)  {
                return date;
            }
        }
        
        throw new ParseException("the date string can not be parse: " + json, 0);
    }
    
    static Long minute  = 60000l;
    static Long minute5 = 5 * minute;
    static Long hour    = 60 * minute;
    static Long day     = 24 * hour;
    static Long month   = 30 * day;
    static Long year    = 12 * month;

    public static String transTimeStamp(Timestamp t) {
        if (t == null) {
            return "";
        }
        Long getTime = t.getTime();
        Long nowTime = System.currentTimeMillis();
        Long distance = nowTime - getTime;

        if (distance < minute5) {
            return "\u521A\u521A";
        } else if (distance < hour) {
            return distance / minute + "\u5206\u949F\u524D";
        } else if (distance < day) {
            return distance / hour + "\u5C0F\u65F6\u524D";
        } else if (distance < month) {
            return distance / day + "\u5929\u524D";
        } else if (distance < year) {
            return distance / month + "\u6708\u524D";
        } else {
            return distance / year + "\u5E74\u524D";
        }
    }
    
    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattens[1]);
        return sdf.format(date);
    }

    /**
     * Formats time in milliseconds to hh:mm:ss string format.
     */
    public static String formatMillis(int millis) {
        String result = "";
        int hr = millis / 3600000;
        millis %= 3600000;
        int min = millis / 60000;
        millis %= 60000;
        int sec = millis / 1000;
        if (hr > 0) {
            result += hr + ":";
        }
        if (min >= 0) {
            if (min > 9) {
                result += min + ":";
            } else {
                result += "0" + min + ":";
            }
        }
        if (sec > 9) {
            result += sec;
        } else {
            result += "0" + sec;
        }
        return result;
    }
}
